<?php

namespace App\Http\Controllers\Admin;

use App\Model\Advloction;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class AdvController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $item = Advloction::orderBy('id','desc')->paginate();
        return view('admin.adv.adv-list',compact('item'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.adv.adv-add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required'
        ],[
            'title.required' => '请填写广告位名称'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }
        $m = new Advloction();
        $m->title = $request->input('title');
        $m->width = $request->input('width');
        $m->height = $request->input('height');
        $m->description = $request->input('description');
        $m->code = $request->input('code');
        if ($m->save()){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Advloction::where('id',$id)->first();
        return view('admin.adv.adv-edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required'
        ],[
            'title.required' => '请填写广告位名称'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }
        $m = Advloction::find($id);
        $m->title = $request->input('title');
        $m->width = $request->input('width');
        $m->height = $request->input('height');
        $m->description = $request->input('description');
        $m->code = $request->input('code');
        if ($m->save()){
            return $this->success('更改成功');
        }else{
            return $this->error('更改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Advloction::where('id',$id)->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}
